package com.cysnake.easyyi.activity;

import java.util.ArrayList;

import com.cysnake.easyyi.R;
import com.cysnake.easyyi.Dao.GuaciDao;
import com.cysnake.easyyi.tools.CommonTool;
import com.cysnake.easyyi.vo.GuaVO;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ZhanBuResActivity extends Activity {

	String benInfo;
	String bianInfo;
	ArrayList<Integer> guaList;

	TextView firstText;
	TextView secondText;
	TextView firstNameText;
	TextView secondNameText;

	ImageView firstUpImage;
	ImageView firstDownImage;
	ImageView secondUpImage;
	ImageView secondDownImage;

	GuaciDao guaDao;

	String dec;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zhanbures);
		guaDao = new GuaciDao(ZhanBuResActivity.this);
		getIntentInfo();
		modify();
		int i = countBianYao(guaList);
		processGuaInfo(i);

	}

	private void modify() {
		modifyImage();
		modifyText();

	}

	private void modifyImage() {
		firstUpImage = (ImageView) findViewById(R.id.zhanbures_image_first_up);
		firstDownImage = (ImageView) findViewById(R.id.zhanbures_image_first_down);
		secondUpImage = (ImageView) findViewById(R.id.zhanbures_image_second_up);
		secondDownImage = (ImageView) findViewById(R.id.zhanbures_image_second_down);
	}

	private void modifyText() {
		firstText = (TextView) findViewById(R.id.zhanbures_textview_first);
		secondText = (TextView) findViewById(R.id.zhanbures_textview_second);
		firstNameText = (TextView) findViewById(R.id.zhanbures_text_name_first);
		secondNameText = (TextView) findViewById(R.id.zhanbures_text_name_second);
	}

	@SuppressWarnings("unchecked")
	private void getIntentInfo() {
		Intent intent = getIntent();
		benInfo = intent.getStringExtra("benInfo");
		bianInfo = intent.getStringExtra("bianInfo");

		guaList = (ArrayList<Integer>) intent.getSerializableExtra("guaList");
		Log.i("test", "tarasdfasdf         " + benInfo);
		Log.i("test", "tarasdfasdf         " + bianInfo);
		Log.i("test", "tarasdfasdf         " + guaList);
	}

	private int countBianYao(ArrayList<Integer> list) {
		int result = 0;
		for (Integer i : list) {
			if (i == 6 || i == 9) {
				result++;
			}
		}
		return result;
	}

	private void processGuaInfo(int i) {
		GuaVO guaVO;
		String temp;

		int con = 0;
		switch (i) {
		case 0:
			guaDao = guaDao.open();
			guaVO = guaDao.getGuaByName(benInfo);
			updateImage(benInfo, firstUpImage, firstDownImage);
			guaDao.close();
			firstNameText.setText(benInfo);
			temp = getYaoText(guaVO, 0);
			setGuaText(firstText, temp);
			break;

		case 1:
			guaDao = guaDao.open();
			guaVO = guaDao.getGuaByName(benInfo);
			updateImage(benInfo, firstUpImage, firstDownImage);
			guaDao.close();
			firstNameText.setText(benInfo);
			for (int j = 1; j < 7; j++) {
				int yao = guaList.get(j - 1);
				if (yao == 9 || yao == 6) {
					temp = getYaoText(guaVO, j);
					setGuaText(firstText, temp);
					break;
				}
			}

			break;
		case 2:
			guaDao = guaDao.open();
			guaVO = guaDao.getGuaByName(benInfo);
			updateImage(benInfo, firstUpImage, firstDownImage);
			guaDao.close();
			firstNameText.setText(benInfo);
			con = 0;
			for (int j = 1; j < 7; j++) {
				int yao = guaList.get(j - 1);

				if (con == 0 && (yao == 9 || yao == 6)) {
					temp = getYaoText(guaVO, j);
					setGuaText(secondText, temp);
					con = 1;
					continue;
				}
				if (con == 1 && (yao == 9 || yao == 6)) {
					temp = getYaoText(guaVO, j);
					setGuaText(firstText, temp);
					break;
				}
			}

			break;
		case 3:
			guaDao = guaDao.open();
			guaVO = guaDao.getGuaByName(benInfo);
			updateImage(benInfo, firstUpImage, firstDownImage);
			temp = getYaoText(guaVO, 0);
			setGuaText(firstText, temp);

			guaVO = guaDao.getGuaByName(bianInfo);
			updateImage(bianInfo, secondUpImage, secondDownImage);
			temp = getYaoText(guaVO, 0);
			setGuaText(secondText, temp);

			guaDao.close();
			firstNameText.setText(benInfo);
			secondNameText.setText(bianInfo);

			break;
		case 4:
			guaDao = guaDao.open();
			guaVO = guaDao.getGuaByName(bianInfo);
			updateImage(bianInfo, firstUpImage, firstDownImage);
			guaDao.close();
			firstNameText.setText(bianInfo);
			con = 0;
			for (int j = 1; j < 7; j++) {
				int yao = guaList.get(j - 1);

				if (con == 0 && (yao == 7 || yao == 8)) {
					temp = getYaoText(guaVO, j);
					setGuaText(firstText, temp);
					con = 1;
					continue;
				}
				if (con == 1 && (yao == 7 || yao == 8)) {
					temp = getYaoText(guaVO, j);
					setGuaText(secondText, temp);
					break;
				}
			}

			break;
		case 5:
			guaDao = guaDao.open();
			guaVO = guaDao.getGuaByName(bianInfo);
			updateImage(bianInfo, firstUpImage, firstDownImage);
			guaDao.close();
			firstNameText.setText(bianInfo);
			for (int j = 1; j < 7; j++) {
				int yao = guaList.get(j - 1);
				if (yao == 7 || yao == 8) {
					temp = getYaoText(guaVO, j);
					setGuaText(firstText, temp);
					break;
				}
			}

			break;
		case 6:
			if (benInfo.equals("乾") || benInfo.equals("坤")) {
				guaDao = guaDao.open();
				guaVO = guaDao.getGuaByName(bianInfo);
				updateImage(benInfo, firstUpImage, firstDownImage);
				guaDao.close();
				firstText.setText(guaVO.getComment());
				firstNameText.setText(benInfo);
			} else {
				guaDao = guaDao.open();
				guaVO = guaDao.getGuaByName(bianInfo);
				updateImage(bianInfo, firstUpImage, firstDownImage);
				guaDao.close();
				temp = getYaoText(guaVO, 0);
				setGuaText(firstText, temp);
				firstNameText.setText(bianInfo);

			}

			break;
		default:
			break;
		}

	}

	private void setGuaText(TextView view, String text) {
		view.setText(text);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(ZhanBuResActivity.this)
						.setTitle(R.string.guainfo_title).setMessage(dec)
						.setPositiveButton(R.string.button_comfirm, null)
						.show();
			}
		});
	}

	private String getYaoText(GuaVO gua, int i) {
		String result;
		switch (i) {
		case 0:
			dec = gua.getTitledec();
			result = gua.getTitle();
			break;
		case 1:
			dec = gua.getFirstdec();
			result = gua.getFirst();
			break;
		case 2:
			dec = gua.getSeconddec();
			result = gua.getSecond();
			break;
		case 3:
			dec = gua.getThirddec();
			result = gua.getThird();
			break;
		case 4:
			dec = gua.getFourthdec();
			result = gua.getFourth();
			break;
		case 5:
			dec = gua.getFifthdec();
			result = gua.getFifth();
			break;
		case 6:
			dec = gua.getLastdec();
			result = gua.getLast();
			break;
		default:
			result = null;

		}
		return result;
	}

	private void updateImage(String name, ImageView upImage, ImageView downImage) {
		String guaNameDec = guaDao.getGuaNameDec(name);
		String upText = guaNameDec.substring(0, 1);
		String downText = guaNameDec.substring(2, 3);
		CommonTool.setImg(upText, upImage);
		CommonTool.setImg(downText, downImage);
	}

}
